package com.lengtouzai.databaseexp.view.remarkcomponent;


import com.lengtouzai.databaseexp.entity.Remark;
import com.lengtouzai.databaseexp.entity.User;
import com.lengtouzai.databaseexp.event.OnRemarkAddEvent;
import com.lengtouzai.databaseexp.event.OnRemarkDeleteEvent;
import com.lengtouzai.databaseexp.view.main.MainView;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.html.Article;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import io.jmix.core.DataManager;
import io.jmix.core.security.CurrentAuthentication;
import io.jmix.flowui.UiEventPublisher;
import io.jmix.flowui.component.textfield.TypedTextField;
import io.jmix.flowui.kit.component.button.JmixButton;
import io.jmix.flowui.model.InstanceContainer;
import io.jmix.flowui.model.InstanceLoader;
import io.jmix.flowui.view.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Route(value = "remark-component", layout = MainView.class)
@ViewController("exp_RemarkComponent")
@ViewDescriptor("remark-component.xml")
public class RemarkComponent extends StandardView {
    @ViewComponent
    private Article content;
    @ViewComponent
    private Article user;
    @Autowired
    private DataManager dataManager;
    @ViewComponent
    private Article receiver;
    @ViewComponent
    private HorizontalLayout toUser;
    @ViewComponent
    private VerticalLayout replyContainer;
    private Remark remark;
    @Autowired
    private CurrentAuthentication currentAuthentication;
    @ViewComponent
    private TypedTextField<Object> replyInput;
    @Autowired
    private UiEventPublisher uiEventPublisher;

    public void setRemark(Remark remark) {
        this.remark = remark;
        user.setText(remark.getBelongs().getUsername());
        if (remark.getToRemark() != null) {
            receiver.setText(remark.getToRemark().getBelongs().getUsername());
            toUser.setVisible(true);
        }
        content.setText(remark.getContent());
    }

    @Subscribe(id = "reply", subject = "clickListener")
    public void onReplyClick(final ClickEvent<JmixButton> event) {
        replyInput.setHelperText("Reply to " + remark.getBelongs().getUsername());
        replyContainer.setVisible(true);
    }

    @Subscribe(id = "cancel", subject = "clickListener")
    public void onCancelClick(final ClickEvent<JmixButton> event) {
        replyContainer.setVisible(false);
        replyInput.clear();
    }

    @Subscribe(id = "delete", subject = "clickListener")
    public void onDeleteClick(final ClickEvent<JmixButton> event) {
        List<Remark> replies = dataManager.load(Remark.class)
                .query("select e from exp_Remark e where e.toRemark = :remark")
                .parameter("remark", remark).list();
        for (Remark reply : replies) {
            dataManager.remove(reply);
        }
        dataManager.remove(remark);
        uiEventPublisher.publishEvent(new OnRemarkDeleteEvent(this));
    }

    @Subscribe(id = "send", subject = "clickListener")
    public void onSendClick(final ClickEvent<JmixButton> event) {
        Remark reply = dataManager.create(Remark.class);
        reply.setBelongs((User) currentAuthentication.getUser());
        reply.setCreatedAt(LocalDateTime.now());
        reply.setToRemark(remark);
        reply.setContent(replyInput.getValue());
        reply.setToJournal(remark.getToJournal());
        dataManager.save(reply);

        uiEventPublisher.publishEvent(new OnRemarkAddEvent(this));
        replyContainer.setVisible(false);
    }
}